﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaskManage
{
    public partial class FormTaskSearch : Form
    {
        public FormTaskSearch()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Clear();
            this.dataGridView1.DataSource = null;

            string tid = this.textBoxTaskId.Text.ToString();
            if (tid == "")
            {
                MessageBox.Show("任务编号不能为空！", "提示");
                return;
            }
            string qstr = "SELECT task_id,task_name,task_pubTime,task_pubId,task_revId,task_staTime,task_endTime,task_content,task_revBg FROM tasks_info WHERE task_id = '" + tid + "'";
            DBConnect dbConn = new DBConnect();
            DataSet ds = dbConn.Query(qstr);
            if (ds == null) return ;
            DataTable dt = ds.Tables[0];
            DataRow irow = dt.Rows[0];
            int idx = this.dataGridView1.Rows.Add();

            for (int j = 0; j < dt.Columns.Count - 1; j++)
            {
                this.dataGridView1.Rows[idx].Cells[j].Value = irow[j].ToString();
            }
            this.dataGridView1.Rows[idx].Cells[3].Value = this.getNameById(irow[3].ToString());
            this.dataGridView1.Rows[idx].Cells[4].Value = this.getNameById(irow[4].ToString());
        }

        private string getNameById(string id)
        {
            string name;
            DBConnect dbConn = new DBConnect();
            DataSet ds = dbConn.Query("SELECT uname FROM users_info WHERE uid = '" + id + "'");
            DataTable dt = ds.Tables[0];
            DataRow row = dt.Rows[0];
            name = row[0].ToString();
            return name;
        }

        private string getIdByName(string name)
        {
            string id;
            DBConnect dbConn = new DBConnect();
            DataSet ds = dbConn.Query("SELECT uid FROM users_info WHERE uname = '" + name + "'");
            DataTable dt = ds.Tables[0];
            DataRow row = dt.Rows[0];
            id = row[0].ToString();
            return id;
        }
    }
}
